#include "cv.h"
#include "highgui.h"

#include <iostream>
#include <time.h>
using namespace std;

int tr;

void detectFigure(IplImage* image, IplImage* imageEmpty, int* x, int* y, bool* isFigure)
{
	//IplImage* bitImage=cvCreateImage(cvSize(image1->width,image1->height),image1->depth,1);
	//IplImage* maskImage=cvCreateImage(cvSize(image1->width,image1->height),image1->depth,1);
	IplImage* diffImage=cvCreateImage(cvSize(image->width,image->height),image->depth,1);
	
    //Differences with actual and last image
	cvAbsDiff(image,imageEmpty,diffImage);
	
	for(int row = 0; row < 8; row++)
		for(int column = 0; column < 8; column++)
		{
			CvRect rect = cvRect(x[column], y[row], x[column + 1] - x[column], y[row + 1] - y[row]);
			cvSetImageROI(diffImage, rect);
			
			/*printf("index:%d x1:%d y1:%d x2:%d y2:%d val0:%f val1:%f val2:%f val3:%f\n", row*8 + column +1, x[column], y[row], x[column+1], y[row+1], cvSum(diffImage).val[0], cvSum(diffImage).val[1], cvSum(diffImage).val[2], cvSum(diffImage).val[3]);
			
			IplImage *img2 = cvCreateImage(cvGetSize(diffImage),
						                  diffImage->depth,
			                              diffImage->nChannels);

			cvCopy(diffImage, img2, NULL);
			printf("\nheight: %d width: %d \n", img2->height,img2->width);
			cvShowImage( "Example3", img2 );
            cvReleaseImage( &img2 );
			cvWaitKey(0);*/

			if(cvSum(diffImage).val[0] > 20000.0)
			{
				isFigure[row*8 + column] = true;
			}
			else
			{
				isFigure[row*8 + column] = false;
			}
			cvResetImageROI(diffImage);
		}
	
	//cvShowImage( "Example3", diffImage );
}



void detectFigureChange(IplImage* imageBegin, IplImage* imageEnd, IplImage* imageEmpty, int* x, int* y, bool* figureChange)
{
	bool isFigureEnd[64];
	detectFigure(imageEnd, imageEmpty, x, y, isFigureEnd);

	bool isFigureBegin[64];
	detectFigure(imageBegin, imageEmpty, x, y, isFigureBegin);

	for(int index = 0; index < 64; index++)
	{
		if(isFigureEnd[index] && !isFigureBegin[index])
		{
			figureChange[index] = true;
		}
		else
		{
			figureChange[index] = false;
		}
	}
}


int main( int argc, char** argv )
{
	cvNamedWindow("ImageBegin",1);
	cvNamedWindow("ImageEnd",1);
	//cvCreateTrackbar("Threshold","Example3",&tr,255,NULL);
    /* data structure for the image */

    IplImage *imageBegin;
	IplImage *imageEnd;
	IplImage *imageEmpty; 
	
	imageBegin = cvLoadImage( "c:\\Gornja23.jpg", CV_LOAD_IMAGE_GRAYSCALE );
	imageEnd = cvLoadImage( "c:\\Gornja24.jpg", CV_LOAD_IMAGE_GRAYSCALE );
	imageEmpty = cvLoadImage( "c:\\Gornja.jpg", CV_LOAD_IMAGE_GRAYSCALE );

	//int x[] = {0, 35, 78, 120, 164, 206, 248, 291, 331};
	//int y[] = {0, 42, 84, 127, 169, 211, 253, 296, 339};

	int x[] = {125, 170, 219, 267, 318, 364, 414, 463, 511};
	int y[] = {49, 95, 146, 193, 243, 290, 341, 390, 437};

	bool isFigure[64];

	detectFigure(imageBegin, imageEmpty, x ,y, isFigure);	
	
	printf("\n\n ImageBegin");
	for(int row = 0; row < 8; row++)
	{
		printf("\n");
		for(int column = 0; column < 8; column++)
		{
			printf(" %d", (isFigure[row*8 + column])?1:0);
		}
	}
    /* wait until user press a key */

	detectFigure(imageEnd, imageEmpty, x ,y, isFigure);	
	
	printf("\n\n ImageEnd");
	for(int row = 0; row < 8; row++)
	{
		printf("\n");
		for(int column = 0; column < 8; column++)
		{
			printf(" %d", (isFigure[row*8 + column])?1:0);
		}
	}
    /* wait until user press a key */

    cvShowImage( "ImageBegin", imageBegin );
	cvShowImage( "ImageEnd", imageEnd );
    cvWaitKey(0);

	///////////////////////////////////////////////////////////////////////////////////////////
	
	bool figureChange[64];
	detectFigureChange(imageBegin, imageEnd, imageEmpty, x, y, figureChange);

	printf("\n");
	printf("\n");
	for(int row = 0; row < 8; row++)
	{
		printf("\n");
		for(int column = 0; column < 8; column++)
		{
			printf(" %d", (figureChange[row*8 + column])?1:0);
		}
	}
  
    /* free memory */
	cvWaitKey(0);

    cvDestroyWindow("ImageBegin");
	cvDestroyWindow("ImageEnd");

    cvReleaseImage(&imageBegin);
	cvReleaseImage(&imageEnd);
	cvReleaseImage(&imageEmpty);

    return 0;

}


